#!/bin/sh
# - tbranch
# - tarch
# - plan
# - base_rpm_url
# - check_rpm_url
# - base_rpms
# - check_rpms
## Oecp is a tool to check the driver kabi in openeuler released kabi whitelist or not.
## Check kernel kabi in whitelist or not.
## Check pkgs compatibility change.
## Check the influence of pkg compatibility change.

. $LKP_SRC/lib/upload.sh

report=/tmp/oecp
CLI_SCRIPT=${HOME}/oecp
PLAN_PATH=${CLI_SCRIPT}/oecp/conf/plan
base_pkg_dir=${HOME}/pkg_download
LOG=/var/log/oecp

function deploy_oecp_tool() {
  echo "========== Start install oecp tool =========="
  local oecp_url="git://${GIT_SERVER}/oecp"
  git clone -q $oecp_url $CLI_SCRIPT
  pip3 install -r ${CLI_SCRIPT}/requirement -i https://pypi.tuna.tsinghua.edu.cn/simple
  echo "========== Install oecp done =========="
}


function wget_check_rpms() {
  mkdir -p ${base_pkg_dir}/{base_dir,check_dir}
  echo "========== start download all base rpms =========="
  if [ ! -n "${base_rpms}" ]; then
    wget -r -nd -np -L -nH --cut-dirs=1 -nv -P ${base_pkg_dir}/base_dir -R *debuginfo*.rpm,*debugsource*.rpm,*.src.rpm -A *.rpm ${base_rpm_url} || echo "
download ${base_rpm_url} all rpms error"
  else
    for base_rpm in ${base_rpms}; do
      wget -P ${base_pkg_dir}/base_dir -nv "${base_rpm_url}/${base_rpm}" || echo "${base_rpm_url}/${base_rpm} download error"
    done
  fi
  echo "========== start download all check rpms =========="
  if [ ! -n "${check_rpms}" ]; then
    wget -r -nd -np -L -nH --cut-dirs=1 -nv -P ${base_pkg_dir}/check_dir -R *debuginfo*.rpm,*debugsource*.rpm,*.src.rpm -A *.rpm ${check_rpm_url} || echo
 "download ${check_rpm_url} all rpms error"
  else
    for check_rpm in ${check_rpms}; do
      wget -P ${base_pkg_dir}/check_dir -nv "${check_rpm_url}/${check_rpm}" || echo "${check_rpm_url}/${check_rpm} download error"
    done
  fi
}


function run_oecp_check() {
  echo "========== Start run oecp check =========="
  if [[ ! "$(ls -A ${base_pkg_dir}/base_dir | grep '.rpm')" && "$(ls -A ${base_pkg_dir}/check_dir | grep '.rpm')" ]]; then
    echo "Not get last version rpms, oecp run with check rpms..."
    python3 ${CLI_SCRIPT}/cli.py -p ${PLAN_PATH}/${plan}.json ${base_pkg_dir}/check_dir ${base_pkg_dir}/check_dir --branch-arch ${tbranch}-${tarch}
  elif [[ "$(ls -A ${base_pkg_dir}/base_dir | grep '.rpm')" && "$(ls -A ${base_pkg_dir}/check_dir | grep '.rpm')" ]]; then
    echo "Oecp run compare base rpms with check rpms..."
    python3 ${CLI_SCRIPT}/cli.py -p ${PLAN_PATH}/${plan}.json ${base_pkg_dir}/base_dir ${base_pkg_dir}/check_dir --branch-arch ${tbranch}-${tarch}
  else
    echo "Not download check rpms, can't run oecp to compare check rpms."
  fi
  
  find $report -name "osv.json"|xargs mv -t $report
  echo "========== Finish oecp check =========="
}



echo "========== Start oecp check ${plan} task =========="
deploy_oecp_tool
wget_check_rpms
run_oecp_check

cmp_result=1
if [ -f ${report}/osv.json ]; then
  diff_num=`cat ${report}/osv.json | grep diff_num | awk -F ":" '{print $2}'`
  if [ $diff_num -eq 0 ]; then
    cmp_result=0
  fi
  upload_files -t oecp-results ${report}/osv.json
else
  echo "Not found the output report: osv.json"
fi

if [ -d ${report}/details_analyse ]; then
  upload_files -t oecp-results ${report}/details_analyse
fi

echo test_result ${cmp_result} > $TMP_RESULT_ROOT/oecp

